package com.bite.mvcdemo.controller;

import com.bite.mvcdemo.model.User;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/response")
@Controller
public class ResponseController {
    @RequestMapping("/returnHtmlPage")
    public String returnHtmlPage(){
        return "/hello.html";
    }

    @ResponseBody
    @RequestMapping("/returnData")
    public String returnData(){
        return "aahahhahaha";
    }

    @ResponseBody
    @RequestMapping("/returnHtml")
    public String returnHtml(){
        return "<h1>returnHtml</h1>";
    }

    @ResponseBody
    @RequestMapping("/returnJson")
    public User returnJson(){
        User user = new User();
        user.setName("zhangsan");
        user.setAge(18);
        return user;
    }

    @ResponseBody
    @RequestMapping("/setStatus")
    public User setStatus(HttpServletResponse response){
        User user = new User();
        user.setName("zhangsan");
        user.setAge(18);
        response.setStatus(500);
        return user;
    }

    @ResponseBody
    @RequestMapping(value = "/setHeader", produces = "application/json")
    public String setHeader(){
        return "{\"success\":true}";
    }

    @ResponseBody
    @RequestMapping("/setHeader2")
    public String setHeader2(HttpServletResponse response){
        response.setHeader("My-Key", "myValue");
        //设置Cookie
        Cookie cookie = new Cookie("key", "value");
        response.addCookie(cookie);
        return "success";
    }


}
